extends Attribute


class_name ExperienceRangeAttribute
"""
用于角色等级的属性
"""


## 当前等级
@export var level: int = 0:
    set(v):
        level = __set_value("level", v)
        event_property_changed.emit(self, "level", level)
## 升级下一级时需要的值
@export var next_value: int = 0:
    set(v):
        next_value = __set_value("next_value", v)
        event_property_changed.emit(self, "next_value", next_value)
## 最大等级
@export var max_level: int = 0:
    set(v):
        max_level = __set_value("max_level", v)
        event_property_changed.emit(self, "max_level", max_level)
## 每升一级需要的成长值
@export var grow_value: int = 0:
    set(v):
        grow_value = __set_value("grow_value", v)
        event_property_changed.emit(self, "grow_value", grow_value)

func __set_value(property: String, new_value: int) -> int:
    match property:
        "value":
            if new_value >= next_value:
                return value
        "level":
            return min(new_value, max_level)
    return new_value

# 返回当前的空余量
func space() -> int:
    return next_value - value

# 获得经验
func increase(increment: int) -> void:
    var increment_value: int = increment
    var total: int = 0
    while increment_value > 0:
        var space_value: int = space()
        # 还有容量
        if space_value > increment_value:
            value += increment_value
            total += increment_value
            break
        if max_level > 0 and level >= max_level:
            break
        # 没有容量,升一级
        value = 0
        next_value += grow_value
        value += 1
        increment_value -= space_value
        total += space_value
